#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


 /**
  * Note: The returned array must be malloced, assume caller calls free().
  */

int Treesize(struct TreeNode* root)
{
    return root == NULL ? 0 : Treesize(root->left) + Treesize(root->right) + 1;
}
void _preOrder(struct TreeNode* root, int* a, int* i)
{
    if (root == NULL)
    {
        return;
    }
    a[*i] = root->val;
    ++(*i);
    _preOrder(root->left, a, i);
    _preOrder(root->right, a, i);

}

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int size = Treesize(root);
    int* a = (int*)malloc(size * sizeof(int));
    int i = 0;
    _preOrder(root, a, &i);
    *returnSize = size;
    return a;
}